<?php
namespace app\index\model;
use think\Model;

class Card extends Model 
{
	/**
	 *  发帖 保存数据到数据库,返回插入数据的主键自增id 失败返回0
	 * @param $data
	 * @return false|int|mixed
	 */
	public function saveCard ($data)
	{
		$id = $this->save($data);
		if($id > 0){
			return $this->id;//这里的id是主键,如果主键是user_id,则返回 $this->user_id; 
		}else{
			return 0;
		}
		
	}

    /**
     * //查询所有的帖子 默认通过发帖时间
     * @return mixed
     */
    public function queryAllCardByTime ($where)
    {
        $where["c.is_first"] = 0;
        $list = $this->alias("c")
            ->join("fly_category ca","c.category_id=ca.id")
            ->join("fly_user u","c.user_id=u.id")
            ->field(['c.*',"ca.category_name","u.name username","u.level","u.image"])
            ->order("create_time desc")
            ->where($where)->paginate(10);
//            ->select();
//        dump($list);die;
        if(empty($list[0])){
            $arr['data']=[];
            $arr['page']=[];
            return $arr;
        }else{
            foreach($list as $key => $val){
                $data[$key] = $val->data;
            }
            $arr['data']=$data;
            $arr['page']=$list->render();
            return $arr;
        }

    }

    /**
     * //查询所有的帖子 默认通过评论数量
     * @return mixed
     */
    public function queryAllCardByEvaluate ($where)
    {
        $where["c.is_first"] = 0;
        $list = $this->alias("c")
            ->join("fly_category ca","c.category_id=ca.id")
            ->join("fly_user u","c.user_id=u.id")
            ->field(['c.*',"ca.category_name","u.name username","u.level","u.image"])
            ->order("evaluate_num desc")
            ->where($where)->paginate(10);
//            ->select();
//        dump($list);die;
        if(empty($list[0])){
            $arr['data']=[];
            $arr['page']=[];
            return $arr;
        }else{
            foreach($list as $key => $val){
                $data[$key] = $val->data;
            }
            $arr['data']=$data;
            $arr['page']=$list->render();
            return $arr;
        }
    }

    /**
     * 查询所有置顶的帖子 限制5条数据
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function queryAllCardByFirst ()
    {
        $list = $this->alias("c")
            ->join("fly_category ca","c.category_id=ca.id")
            ->join("fly_user u","c.user_id=u.id")
            ->field(['c.*',"ca.category_name","u.name username","u.level","u.image"])
            ->order("evaluate_num desc , browse desc")
            ->where("c.is_first = 1")
            ->limit(5)
            ->select();
//        dump($list);die;
        if(empty($list)){
            return $list;
        }else{
            foreach($list as $key => $val){
                $data[$key] = $val->data;
            }
            return $data;
        }
    }

    /**
     * 通过帖子id 查看帖子详情
     * @param $id
     * @return array|false|object|\PDOStatement|string|Model
     */
    public function queryCardByid ($id)
    {
//        $info = $this->find($id);
        $info = $this->alias("c")
            ->join("fly_category ca","c.category_id=ca.id")
            ->join("fly_user u","c.user_id=u.id")
            ->field(['c.*',"ca.category_name","u.name username","u.level","u.image"])
            ->where("c.id=$id")
            ->find();
//        dump($info);die;
        if(empty($info)){
            return $info;
        }else{
            return $info->data;
        }
    }

    /**
     * 帖子某字段 $field 自增$num
     * @param $field
     * @param $num
     */
    public function setIncCard ($where,$field,$num)
    {
        $this->where($where)->setInc($field,$num);
    }

    /**
     * 帖子某字段 $field 自减$num
     * @param $field
     * @param $num
     */
    public function setDecCard ($where,$field,$num)
    {
        $this->where($where)->setDec($field,$num);
    }

    /**
     * 通过用户id查看他的所有帖子
     * @param $id
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function queryAllCardById ($id)
    {
        $list = $this->alias("c")
            ->join("fly_category ca","c.category_id=ca.id")
//            ->join("fly_user u","c.user_id=u.id")
            ->field(['c.*',"ca.category_name"])
            ->order("create_time")
            ->where("c.user_id = $id")->paginate(10);
//            ->select();
//        dump($list);die;
        if(empty($list[0])){
            $arr['data']=[];
            $arr['page']=[];
            return $arr;
        }else{
            foreach($list as $key => $val){
                $data[$key] = $val->data;
            }
            $arr['data']=$data;
            $arr['page']=$list->render();
            return $arr;
        }
    }

    /**0
     * 根据$where条件数组,跟新一条帖子数据
     * @param $where
     * @param $data
     * @return $this
     */
    public function updateCard ($where,$data)
    {
        return $this->where($where)->update($data);
    }
}

?>
